define(function(require, exports, module) {
	"use strict";
	require("../../dep/mui/js/mui.min.js");
	require("../jquery-3.1.1.min.js");
	var abpUse = require("../abp.js");
	var template = require("template");
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回清除缓存
	var dingTalkNav = require('dd_config');

	function backList() {
		document.location.href = '../../src/newCheckStock/checkBegin.html?dd_nav_bgcolor=FFFF943E';
	}
	//钉钉返回
	dingTalkNav.LeftNavBar(backList);
	//ad
	dingTalkNav.leftAndroid(backList);
	var abp = abpUse.abpstart;
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	var check_id = sessionStorage.getItem('check_id');
	var option = {};
	var checkData = {};
	var dataModel = {};
	var btnStatus = true;
	$(function() {
		dataModel = {
			"Id": check_id
		};
		abp.services.app.checkList.getModel(dataModel, option)
			.done(function(data) {
				checkData = {
					"Id": check_id,
					"WhId": data.whId,
					"CatalogId": data.catalogId,
					"StatusId": data.statusId,
					"SerialNo": data.serialNo,
					"SerialDate": data.serialDate,
					"StorageId": data.storageId,
					"DeptId": data.deptId,
					"Notes": data.notes
				};
			})
			.fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		dataModel = {
			"id": check_id,
			"PageIndex": 1,
			"PageSize": 10
		};
		var profitValue = 0;
		var lossValue = 0;
		abp.services.app.checkList.getDetailPage(dataModel, option)
			.done(function(data) {
				$("#goodsCount").text(data.totalCount);
				$("#inventoryProfit").text();
				$("#inventoryLoss").text();
				var items = data.items;
				$.each(items, function(i, v) {
					v.checkLossNumber = parseFloat(v.checkLossNumber).toFixed(2);
					v.checkOverageNumber = parseFloat(v.checkOverageNumber).toFixed(2);
					v.whNumber = v.whNumber.toFixed(2);
					profitValue += v.checkOverageNumber;
					lossValue += v.checkLossNumber;
					if(v.checkNumber < 0) {
						v.checkNumber = 0;
					}

				});
				if(profitValue > 0)
					$("#inventoryProfit").text(parseFloat(profitValue));

				if(lossValue > 0)
					$("#inventoryLoss").text(parseFloat(lossValue));
				var html = template("checkList", data);
				$("#check-goods-items").html(html);
				InitInventory("#check-goods-items");
			})
			.fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
	});
	var SaveCheck = function(statusId) {
		var success = false;
		var CheckDetail = [];
		checkData.StatusId = statusId;
		checkData.CheckDetail = [];
		var goodsItmes = $(".check-good-item");
		for(var i = 0; i < goodsItmes.length; i++) {
			var id = Number($(".check-good-item").eq(i).attr("id"));
			var whNumber = Number($(".check-good-item").eq(i).find("#whNumber").text());
			var checkNumber = Number($(".check-good-item").eq(i).find("#checkNumber").val());
			var checkDetailInput = {
				"Id": id,
				"WhNumber": whNumber,
				"CheckNumber": checkNumber === 0 ? -1 : checkNumber
			};
			checkData.CheckDetail.push(checkDetailInput);
		}
		abp.services.app.checkList.update(checkData, option)
			.done(function(data) {
				console.log("保存成功");
				success = true;
				sessionStorage.setItem('check_id', check_id);
				document.location.href = '../inventoryRecord/inventoryRecordDetail.html';
			})
			.fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		return success;
	};
	mui("body").on("tap", "#DraftCheck", function() {
		SaveCheck(abp.statusId.Draft);
	});
	mui("body").on("tap", "#FinishCheck", function() {
		SaveCheck(abp.statusId.Finish);
	});

	$('body').on('keyup', 'input[name=checkNumber]', function() {
		var checkStatus = true;
		var $tr = $(this).parent().parent();
		var checkNumber = $(this).val().trim();
		var whNumber = $tr.find("#whNumber").text().trim();
		if(parseFloat(checkNumber) > parseFloat(whNumber)) {
			$tr.find("#item-goods-loss").text(0);
			$tr.find("#item-goods-profit").text((parseFloat(checkNumber) - parseFloat(whNumber)).toFixed(2));
		} else {
			$tr.find("#item-goods-profit").text(0);
			$tr.find("#item-goods-loss").text((parseFloat(whNumber) - parseFloat(checkNumber)).toFixed(2));
		}
		if(checkNumber.length === 0 || whNumber.length === 0) {
			$tr.find("#item-goods-loss").text(0);
			$tr.find("#item-goods-profit").text(0);
		}
		InventoryTotal();
		var inputCount = $("input[name=checkNumber]").length;
		for(var i = 0; i < inputCount; i++) {
			if($("input[name=checkNumber]").eq(i).val().trim().length === 0) {
				checkStatus = false;
				break;
			}
		}
		console.log(checkStatus);
		if(checkStatus) {
			$("#check-btn").css("height", "98px");
			$("#FinishCheck").show();
		} else {
			$("#check-btn").css("height", "49px");
			$("#FinishCheck").hide();
		}
	});
	var InitInventory = function(htmlId) {
		var $tr = $(htmlId).find("input[name=checkNumber]");
		var $len = $tr.length;
		var checkStatus = true;
		for(var i = 0; i < $len; i++) {
			if($tr.eq(i).val() == "0") {
				checkStatus = false;
				break;
			}
		}
		if(checkStatus) {
			$("#check-btn").css("height", "98px");
			$("#FinishCheck").show();
		} else {
			$("#check-btn").css("height", "49px");
			$("#FinishCheck").hide();
		}
	};
	var InventoryTotal = function() {
		var $len = $("#check-goods-items li").length;
		var $tr = $("#check-goods-items li");
		var inventoryProfit = 0;
		var inventoryLoss = 0;
		for(var i = 0; i < $len; i++) {
			var lossVal = $tr.eq(i).find("#item-goods-loss").text();
			var profitVal = $tr.eq(i).find("#item-goods-profit").text();
			inventoryProfit += parseFloat(profitVal);
			inventoryLoss += parseFloat(lossVal);
		}
		$("#inventoryProfit").text(inventoryProfit.toFixed(2));
		$("#inventoryLoss").text(inventoryLoss.toFixed(2));
	};
});